﻿using System;
using System.Collections.Generic;

namespace LogHelper.SQLiteORM.TypeConvertor
{
    public class TypeConvertorCollection : Dictionary<Type, IConvertor>
    {
        /// <summary>
        /// 所有类型都必须进行转换
        /// </summary>
        public bool ConvertForce { get; set; }

        public TypeConvertorCollection()
        {
            Add(typeof(int), new ConvertToInt());
            Add(typeof(bool), new ConvertToBool());
        }

        internal object Convert(Type type, object value)
        {
            if (ContainsKey(type))
                return this[type].Convert(value);
            if (ConvertForce)
                throw new NotSupportedException($"不支持类型{type}的转换，请针对类型{type}添加类型转换支持。");
            return value;
        }
    }
}